package com.sgb.kjwl.view.ui.activity.leaseAsk.publish;

import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAskLeaseFactory2Binding;
import com.sgb.kjwl.view.ui.activity.leaseAsk.fragment.AskLeaseFactoryFragment1;
import com.sgb.kjwl.view.ui.activity.leaseAsk.fragment.AskLeaseFactoryFragment2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.fragment.AskLeaseFactoryFragment3;
import com.sgb.kjwl.viewmodel.machine.AskLeaseFactoryModel2;
import com.swgk.core.dialog.CommonDialog;

/**
 * 工厂求租发布
 */
public class AskLeaseFactoryActivity2 extends AppActivity {

    AskLeaseFactoryModel2 viewModel;
    private ActivityAskLeaseFactory2Binding binding;

    private String id = "";
    private AskLeaseFactoryFragment1 fragment1;
    private AskLeaseFactoryFragment2 fragment2;
    private AskLeaseFactoryFragment3 fragment3;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, AskLeaseFactoryActivity2.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        viewModel = ViewModelProviders.of(this).get(AskLeaseFactoryModel2.class);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_ask_lease_factory2);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
        binding.topLayout.titleView.setText("工厂求租发布");
        binding.topLayout.ivBack.setOnClickListener(v ->onBackPressed());
    }

    private void recevierliveData() {
        viewModel.selectFragLiveData.observe(this, data -> {
            replaceFragment(data);
        });
        viewModel.publishLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String str) {
                finish();
                PublishSuccessActivity.start(getActivity(), str, "6");
            }
        });
    }

    @Override
    protected void init() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置沉浸式状态栏的上移问题
        setTileImmerse(true);
        id = getIntent().getStringExtra("id");
        recevierliveData();
        replaceFragment(0);
    }

    private void replaceFragment(int flag) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (flag == 0) {
            if (fragment1 == null) {
                fragment1 = AskLeaseFactoryFragment1.newInstance(id);
                transaction.add(R.id.frag, fragment1);
            }
            hideAllFragment();
            transaction.show(fragment1);
        }
        if (flag == 1) {
            if (fragment2 == null) {
                fragment2 = AskLeaseFactoryFragment2.newInstance(id);
                transaction.add(R.id.frag, fragment2);
            }
            hideAllFragment();
            transaction.show(fragment2);
        }
        if (flag == 2) {
            if (fragment3 == null) {
                fragment3 = AskLeaseFactoryFragment3.newInstance(id);
                transaction.add(R.id.frag, fragment3);
            }
            hideAllFragment();
            transaction.show(fragment3);
        }
        transaction.commit();
    }

    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
        transaction.commit();
    }

    @Override
    public void onBackPressed() {
            initDialog();
            return;
    }

    public void initDialog() {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("提示")
                .setMsg("确定要退出吗？已填写的内容将不会保存！")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        AskLeaseFactoryActivity2.this.finish();
                    }

                    @Override
                    public void onLeftClick() {
                        return;
                    }
                }).show();
    }

}
